package com.example.myFirstWebmagic.dailyAirReport.service;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import us.codecraft.webmagic.Spider;

import com.example.myFirstWebmagic.airQualityForecasting.AirqualityForecastDao;
import com.example.myFirstWebmagic.airQualityForecasting.AirqualityForecastPipeline;
import com.example.myFirstWebmagic.airQualityForecasting.AirqualityForecastProcess;
import com.example.myFirstWebmagic.airQualityForecasting.AreaForecastDao;
import com.example.myFirstWebmagic.airQualityForecasting.AreaForecastPipeline;
import com.example.myFirstWebmagic.airQualityForecasting.AreaForecastProcess;
import com.example.myFirstWebmagic.airQualityForecasting.ProvinceForecastDao;
import com.example.myFirstWebmagic.airQualityForecasting.ProvinceForecastPipeline;
import com.example.myFirstWebmagic.airQualityForecasting.ProvinceForecastProcess;
import com.example.myFirstWebmagic.aspect.LogTag;
import com.example.myFirstWebmagic.company.CompanyBgProcess;
import com.example.myFirstWebmagic.company.CompanyDao;
import com.example.myFirstWebmagic.company.CompanyProcess;
import com.example.myFirstWebmagic.dailyAirReport.dao.DailyAirQualityReportLogRepository;
import com.example.myFirstWebmagic.dailyAirReport.dao.DailyAirQualityReportRepository;
import com.example.myFirstWebmagic.dailyAirReport.entity.EnDailyAirQualityReportLog;
import com.example.myFirstWebmagic.dailyAirReport.entity.DailyAirQualityReportLogPK;
import com.example.myFirstWebmagic.dailyAirReport.processor.NewAirDailyProcessor;
import com.example.myFirstWebmagic.dailyAirReport.processor.NewAirDailyStarttimeProcessor;
import com.example.myFirstWebmagic.hourAirReport.dao.HourAirQualityReportRepository;
import com.example.myFirstWebmagic.hourAirReport.processor.NewAirHourProcess;
import com.example.myFirstWebmagic.pipeline.CompanyBgPipeline;
import com.example.myFirstWebmagic.pipeline.CompanyInfoPipeline;
import com.example.myFirstWebmagic.pipeline.HourMysqlPipeline;
import com.example.myFirstWebmagic.pipeline.MysqlPipeline;
import com.example.myFirstWebmagic.schedule.EnSchedulingSwitch;
import com.example.myFirstWebmagic.schedule.SchedulingSwitchRepository;
import com.example.myFirstWebmagic.util.DateUtil;
import com.example.myFirstWebmagic.waterReport.WaterProcessor;
import com.example.myFirstWebmagic.waterReport.WaterStationMysqlPipeline;
import com.example.myFirstWebmagic.waterReport.WaterStationReportRepository;

@Component("webmagicService")
@Transactional
public class WebmagicServiceImpl implements WebmagicService {
	
	
	@Autowired
	private DailyAirQualityReportRepository dailyAirQualityReportRepository;
	
	@Autowired
	private DailyAirQualityReportLogRepository dailyAirQualityReportLogRepository;
	
	@Autowired
	private SchedulingSwitchRepository schedulingSwitchRepository;
	
	@Autowired
	private HourAirQualityReportRepository hourAirQualityReportRepository;
	
	@Autowired
	private WaterStationReportRepository waterStationReportRepository;
	
	@Autowired
	private AirqualityForecastDao airqualityForecastDao;
	
	@Autowired
	private AreaForecastDao areaForecastDao;
	
	@Autowired
	private ProvinceForecastDao provinceForecastDao;

	@Autowired
	private CompanyDao companyDao;
	
	@Autowired
	private CompanyProcess companyProcess;
	
	@Autowired
	private CompanyInfoPipeline companyInfoPipeline;
	
	@Autowired
	private CompanyBgPipeline companyBgPipeline;
	
	@Autowired
	private CompanyBgProcess companyBgProcess;
	
	
	//要改成执行该方法前先判断日志，在执行爬虫开始前就插入入职，爬取结束后更新日志结果。
	@LogTag
	@Override
	public void airDailyReportStart(String startime) {
		
		//如果startime为空，则爬取最新日期
		if(StringUtils.isEmpty(startime)){
			startime = getRecentlyTime();
		}

		//TODO取出数据库日志表中的日报
		DailyAirQualityReportLogPK logPK=new DailyAirQualityReportLogPK();
		logPK.setSpiderName(NewAirDailyProcessor.class.getName());
		logPK.setTime(startime);
	
			String exception="";
			EnDailyAirQualityReportLog airQualityReportLog=new EnDailyAirQualityReportLog();
			airQualityReportLog.setId(logPK);
			airQualityReportLog.setStatus(EnDailyAirQualityReportLog.EXECUTING_STATUS);
			airQualityReportLog.setException(exception);
			
			try{
				String [] startUrl=new String[]{"http://datacenter.mep.gov.cn/index!getCityMain.action"};
				NewAirDailyProcessor airDailyProcessor=new NewAirDailyProcessor();
				//设置爬取指定日期
				airDailyProcessor.setStartime(startime);
				Spider.create(airDailyProcessor).addUrl(startUrl).addPipeline(new MysqlPipeline(dailyAirQualityReportRepository)).thread(5).run();
				airQualityReportLog.setStatus(EnDailyAirQualityReportLog.SUCCESS_STATUS);
			}catch(Exception e){
				exception=e.getMessage();
				airQualityReportLog.setException(exception);
				airQualityReportLog.setStatus(EnDailyAirQualityReportLog.FAILD_STATUS);
			}
	}

	@LogTag
	@Override
	public void modifySchedule(String schedule, String status) {
		// TODO Auto-generated method stub
		
		EnSchedulingSwitch schedulingSwitch= schedulingSwitchRepository.findByScheduleName(schedule);
		//如果为空，则新建一个开关
		if(null ==schedulingSwitch ){
			schedulingSwitch=new EnSchedulingSwitch();
			schedulingSwitch.setScheduleName(schedule);
			schedulingSwitch.setSswitch(status);
			schedulingSwitchRepository.save(schedulingSwitch);
		}else{
			//如果不为空，则修改开关状态
			schedulingSwitchRepository.updateEnSchedulingSwitchSswitch(status,schedule);
		}
	}

	@LogTag
	@Override
	public void airHourReportStart(String date) {
		// TODO Auto-generated method stub
		//如果startime为空，则爬取今天日期
		if(StringUtils.isEmpty(date)){
			date = DateUtil.getToday();
		}
		String [] startUrl=new String[]{"http://datacenter.mep.gov.cn/index!environmentAirHourDetailCity.action"};
		NewAirHourProcess hourDailyProcessor=new NewAirHourProcess();
		//设置爬取指定日期
		hourDailyProcessor.setDate(date);
		Spider.create(hourDailyProcessor).addUrl(startUrl).addPipeline(new HourMysqlPipeline(hourAirQualityReportRepository)).thread(5).run();
												
	}


	private String getRecentlyTime() {
		String starttime;
		String [] starttimeUrl=new String[]{"http://datacenter.mep.gov.cn/index!getCityMain.action"};
		NewAirDailyStarttimeProcessor airDailyStarttimeProcessor=new NewAirDailyStarttimeProcessor();
		Spider.create(airDailyStarttimeProcessor).addUrl(starttimeUrl).thread(1).run();
		starttime=airDailyStarttimeProcessor.getStartime();
		return starttime;
	}

	@LogTag
	@Override
	public void waterReportStart() {
		// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://58.68.130.147"};
		Spider.create(new WaterProcessor()).addUrl(startUrl).addPipeline(new WaterStationMysqlPipeline(waterStationReportRepository)).thread(1).run();
	}

	@LogTag
	@Override
	public void aqiForecastStart() {
		// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://106.37.208.228:8082/Home/Default"};
		Spider.create(new AirqualityForecastProcess()).addUrl(startUrl).addPipeline(new AirqualityForecastPipeline(airqualityForecastDao)).thread(1).run();
	}

	@LogTag
	@Override
	public void areaAqiForecastStart() {
		// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://106.37.208.228:8082/AreaForecast"};
		Spider.create(new AreaForecastProcess()).addUrl(startUrl).addPipeline(new AreaForecastPipeline(areaForecastDao)).thread(1).run();
	}


	@LogTag
	@Override
	public void provinceAqiForecastStart() {
		// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://106.37.208.228:8082/ProvinceForecast/GetGlobalInfo"};
		Spider.create(new ProvinceForecastProcess()).addUrl(startUrl).addPipeline(new ProvinceForecastPipeline(provinceForecastDao)).thread(1).run();
	}

	@LogTag
	@Override
	public void companyInfo() {
		// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://59.172.182.106/qyjc/jcjgXxOne.rh"};
		// TODO Auto-generated method stub
		Spider.create(companyProcess)
        // TODO 从"http://datacenter.mep.gov.cn/index!getCityMain.action"开始抓
        .addUrl(startUrl)
        .addPipeline(companyInfoPipeline)
        // TODO 开启5个线程抓取
        .thread(5)
        // TODO 启动爬虫
        .run();
	}

	@LogTag
	@Override
	public void companyBg() {
		// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://59.172.182.106/qyjc/jcjgXxOne.rh"};
		// TODO Auto-generated method stub
		Spider.create(companyBgProcess)
        // TODO 从"http://datacenter.mep.gov.cn/index!getCityMain.action"开始抓
        .addUrl(startUrl)
        .addPipeline(companyBgPipeline)
        // TODO 开启5个线程抓取
        .thread(5)
        // TODO 启动爬虫
        .run();
	}

	
	
}
